package com.hxd.mall.utils.open

import android.annotation.SuppressLint
import android.text.SpannableString
import android.text.Spanned
import android.text.style.ImageSpan
import android.widget.TextView
import androidx.annotation.DrawableRes
import androidx.appcompat.content.res.AppCompatResources

/**
 * Create by yinzhengwei on 2019/3/18
 * @Function
 */
@SuppressLint("SetTextI18n")
fun TextView.setNewBankCard(bankCard: String) {
    //对卡号进行脱敏处理
    if (bankCard.isNotEmpty() && bankCard.length >= 4) {
        text = "*** *** *** ${bankCard.substring(bankCard.length - 4, bankCard.length)}"
    } else {
        text = bankCard
    }
}

/**
 * 对身份证进行脱敏处理
 */
@SuppressLint("SetTextI18n")
fun TextView.setIdNum(idNum: String) {
    //对卡号进行脱敏处理
    text = if (idNum.isNotEmpty() && idNum.length >= 4) {
        "${idNum.substring(0, 4)} *** *** ${idNum.substring(idNum.length - 4, idNum.length)}"
    } else {
        idNum
    }
}

fun TextView.setIconAndText(@DrawableRes icon: Int, text: String) {
    SpannableString("  $text").run {
        setSpan(
            ImageSpan(AppCompatResources.getDrawable(context, icon)!!.apply {
                setBounds(
                    0, 0,
                    intrinsicWidth, intrinsicHeight
                )
            }),
            0, 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
        )
        setText(this)
    }
}